if (document.querySelector('.user-logining')) {
  document.querySelector('.user-logining').addEventListener('click', () => {
    let user = document.querySelector('.user')
    if (!user.classList.contains('user-active')) {
      user.classList.add('user-active')
    } else {
      user.classList.remove('user-active')
    }
  })

}

//用户点击退出
document.querySelector('.user-out').addEventListener('click', () => {
  document.querySelector('.modal-out').style.display = 'flex'
})
document.querySelector('.modal-out').addEventListener('click', function (e) {
  if (e.target == this) {
    this.style.display = 'none'
  }
})
document.querySelector('.modal-out form button').addEventListener('click', () => {
  document.querySelector('.modal-out').style.display = 'none'
})


//保存编辑
document.querySelector('.form-top-btn button').addEventListener('click', (e) => {
  e.preventDefault();
  let board = document.querySelector('.selectInp select').value
  let title = document.querySelector('.art-title input').value
  let content = document.querySelector('.art-content textarea').value;
  let un = document.querySelector('.userMsg').value
  let obj = { un, board, title, content }
  if (board == 0 && title.trim() == '' && content.trim() == '') {
    saveGone('saveNoActive', '保存失败,请填写内容')
  } else {
    let saveObj = JSON.parse(localStorage.getItem('saveObj')) || []
    if (saveObj.length > 0) {
      let userIndex = saveObj.findIndex(item => item.un == un)
      if (userIndex === -1) {
        saveObj.push(obj)
      } else {
        saveObj[userIndex] = obj
      }
    } else {
      saveObj = [obj]
    }
    localStorage.setItem('saveObj', JSON.stringify(saveObj))
    saveGone('saveActive', '保存成功')
  }
});

//消息提示函数
function saveGone(classname, text) {
  document.querySelector('.save').classList.add(`${classname}`)
  document.querySelector('.save').textContent = text
  setTimeout(() => {
    document.querySelector('.save').classList.remove(`${classname}`)
  }, 2000)
}


function renderInput() {
  let un = document.querySelector('.userMsg').value
  let saveObj = JSON.parse(localStorage.getItem('saveObj')) || []
  if (saveObj.length > 0) {
    let user = saveObj.find(item => item.un == un)
    document.querySelector('.selectInp select').value = user.board
    document.querySelector('.art-title input').value = user.title
    document.querySelector('.art-content textarea').value = user.content
  }

}
renderInput()

document.querySelector('.form-top-btn input').addEventListener('click', (e) => {
  let board = document.querySelector('.selectInp select').value
  let title = document.querySelector('.art-title input').value
  let content = document.querySelector('.art-content textarea').value;
  if (board == 0 || title.trim() == '' || content.trim() == '') {
    e.preventDefault()
    saveGone('saveNoActive', '上传失败，内容错误')
  } else {
    // console.log(123213)
    let un = document.querySelector('.userMsg').value
    let saveObj = JSON.parse(localStorage.getItem('saveObj')) || []
    if (saveObj.length > 0) {
      let userIndex = saveObj.findIndex(item => item.un == un)
      if (userIndex != -1) {
        saveObj.splice(userIndex, 1)
      }
    }
    localStorage.setItem('saveObj', JSON.stringify(saveObj))
  }


})

document.querySelector('.user ul li:nth-child(1)').addEventListener('click',()=>{
  console.log(2)
  window.location.href="../view/userIndex.php"
})